#!/bin/bash
category="ARP"
cycle=${1:-'60'}
detectionType=${2:-'default'}
log="/tmp/gpu-arp.log"
err="/tmp/gpu-arp.err"

gpuCount=${3:-'8'}
arpAvg=${4:-'0'}

resultcode=0
resultmsg=""

# GPU数量   > $log
gpu_count=$(nvidia-smi --list-gpus | wc -l)
echo "GPU通讯能力测试开始，GPU数量：" $gpu_count > $log

if [[ $gpuCount -ne $gpu_count ]]; then
	resultcode="6"
	resultmsg="GPU quantity error, actual ${gpu_count}, configured ${gpuCount}"
	echo "category=${category};resultcode=${resultcode};resultmsg=${resultmsg};" >> $log
	echo "category=${category};resultcode=${resultcode};resultmsg=${resultmsg};"
	exit 1
fi

# 启动 
cd /root/scripts/gputest/nccl-tests-master/build
./all_reduce_perf -b 128M -e 1G -f 2 -g $gpu_count >> $log


echo "GPU通讯能力测试，执行完成======" >> $log

currAvg=$(grep "Avg bus bandwidth" $log | awk '{print $NF}')

resultmsg="Pref：$currAvg GB/s"

if (( $(echo "$arpAvg > 0" | bc -l) )); then
	resultmsg="Pref：${currAvg}/${arpAvg} GB/s"
fi

# 判断arpAvg的值是否大于0  如果设置了需要返回两个值 如果没设置则直接返回测试值
# 判断 currAvg 是否小于 currAvg
if (( $(echo "$currAvg < $arpAvg" | bc -l) )); then
   resultcode="9"
#    resultmsg="Pref：$currAvg/${arpAvg} GB/s"
fi

#sleep 1
echo "category=${category};resultcode=${resultcode};resultmsg=${resultmsg};data=${currAvg}"

exit 0


